﻿@model OnlineBookstore.Models.BookDetailsInfo
@{
    Layout = null;
    // 统一模型访问方式
    var book = Model ?? new OnlineBookstore.Models.BookDetailsInfo();
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加书籍 - 后台管理</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet">
    <style>
        .admin-container {
            max-width: 800px;
            margin: 2rem auto;
            padding: 2rem;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .form-header {
            border-bottom: 2px solid #eee;
            padding-bottom: 1rem;
            margin-bottom: 2rem;
        }

        .form-label {
            font-weight: 600;
            color: #2c3e50;
        }

        .url-preview {
            max-width: 200px;
            margin-top: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            display: none;
        }
        .input-group-text {
            padding: 0.375rem 0.75rem;
            margin-right: 0.75rem; /* Adjust this value to control the spacing between the symbol and the input */
            font-size: 1rem;
            font-weight: 400;
            line-height: 1.5;
            color: #495057; /* Adjust color as needed */
            text-align: center;
            white-space: nowrap;
            background-color: #e9ecef; /* Background color */
            border: 1px solid #ced4da; /* Border */
            border-radius: 0.25rem 0 0 0.25rem; /* Rounded corners */
        }
        .input-group {
            position: relative;
            display: flex;
            align-items: center;
        }
    </style>
</head>
<body>
    <div class="admin-container">
        <div class="form-header">
            <h2>添加新书</h2>
            <small class="text-muted">请输入完整的书籍信息</small>
        </div>
        @using (Html.BeginForm("Create", "BookDetailsInfo", FormMethod.Post))
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.BookCode)

            <!-- 统一使用局部变量book -->
            <div class="form-group">
                <p class = "form-label">书名</p>
               	@Html.EditorFor(model => model.BookName, new { htmlAttributes = new { @class = "form-control", placeholder = "请输入书名" } })
                @Html.ValidationMessageFor(m => book.BookName, "", new { @class = "text-danger" })
            </div>

            <div class="form-group">
                <p class = "form-label">作者</p>
                @Html.EditorFor(model => model.BookAuthor, new { htmlAttributes = new { @class = "form-control", placeholder = "请输入作者" } })
                @Html.ValidationMessageFor(m => book.BookAuthor, "", new { @class = "text-danger" })
            </div>

            <div class="form-group">
                <p class="form-label">出版社</p>
                @Html.EditorFor(model => model.Bookpress, new { htmlAttributes = new { @class = "form-control", placeholder = "请输入出版社" } })
                @Html.ValidationMessageFor(m => book.Bookpress, "", new { @class = "text-danger" })
            </div>

            <div class="form-group">
                <p class="form-label">价格</p>
                <div class="input-group">
                    <span class="input-group-text">¥</span> @Html.EditorFor(model => model.BookPrice, new { htmlAttributes = new { @class = "form-control", type = "number", step = "0.01", placeholder = "0.00" } })
                </div>
                @Html.ValidationMessageFor(m => book.BookPrice, "", new { @class = "text-danger" })
            </div>

            <div class="form-group">
                <p class="form-label">图片</p>
                @Html.EditorFor(model => model.Bookcover, new
                {
                    htmlAttributes = new
                    {
                        @class = "form-control",
                        placeholder = "https://example.com/cover.jpg ",
                        oninput = "previewCover(this.value)"
                    }
                })

                <img id="coverPreview" class="url-preview" alt="封面预览" style="display:none; max-width:200px;">
                @Html.ValidationMessageFor(m => m.Bookcover, "", new { @class = "text-danger" })
            </div>

            <div class="form-group">
                <p class="form-label">图书分类</p>
                @Html.DropDownListFor(model => model.Booksort, new SelectList(new List<string> { "文学", "科技", "历史", "教育" }), "选择分类", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Booksort, "", new { @class = "text-danger" })
            </div>

            <div class="form-group mt-4">
                <button type="submit" class="btn btn-primary btn-lg w-100">
                    <i class="fas fa-save me-2"></i>确认添加
                </button>
                @Html.ActionLink("返回列表", "GetBookList", null, new { @class = "btn btn-link d-block mt-2 text-center" })
            </div>
        }
    </div>

    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script>
        // 封面链接实时预览功能
        function previewCover(url) {
            const preview = document.getElementById('coverPreview');
            url = url.trim(); // 去除前后空格
            if (url && /^https?:\/\//i.test(url)) {
                preview.src = url;
                preview.style.display = 'block';
            } else {
                preview.style.display = 'none';
            }
        }


    </script>
</body>
</html>